package org.alvin.i18n.newui;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;


public class GoogleMapsGeocodingInJava {
    public static void getHongKongDistrictInfo(String address, String apiKey) {
//        try {
//            String urlStr = "https://maps.googleapis.com/maps/api/geocode/json?address=" + address + "&key=" + apiKey;
//            URL url = new URL(urlStr);
//            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//            connection.setRequestMethod("GET");
//
//            int responseCode = connection.getResponseCode();
//            if (responseCode == HttpURLConnection.HTTP_OK) {
//                BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
//                String inputLine;
//                StringBuilder response = new StringBuilder();
//                while ((inputLine = in.readLine())!= null) {
//                    response.append(inputLine);
//                }
//                in.close();
//
//                JSONObject jsonObject = JSONObject.parseObject(response.toString());
//                if ("OK".equals(jsonObject.getString("status"))) {
//                    JSONArray results = jsonObject.getJSONArray("results");
//                    for (int i = 0; i < results.size(); i++) {
//                        JSONObject result = results.getJSONObject(i);
//                        JSONArray addressComponents = result.getJSONArray("address_components");
//                        for (int j = 0; j < addressComponents.size(); j++) {
//                            JSONObject component = addressComponents.getJSONObject(j);
//                            if (component.containsKey("types")) {
//                                JSONArray types = component.getJSONArray("types");
//                                for (int k = 0; k < types.size(); k++) {
//                                    if ("administrative_area_level_1".equals(types.getString(k))) {
//                                        System.out.println("一级行政区: " + component.getString("long_name"));
//                                    } else if ("administrative_area_level_2".equals(types.getString(k))) {
//                                        System.out.println("二级行政区: " + component.getString("long_name"));
//                                    }
//                                }
//                            }
//                        }
//                    }
//                } else {
//                    System.out.println("地址解析失败: " + jsonObject.getString("status"));
//                }
//            } else {
//                System.out.println("请求失败，状态码: " + responseCode);
//            }
//        } catch (IOException  e) {
//            e.printStackTrace();
//        }
    }

    public static void main(String[] args) {
        String apiKey = "AIzaSyAZUtfkuVFov4TaXJJPMPWI9eHs5RYZpOI";
        String address = "香港铜锣湾轩尼诗道 500 号";
        getHongKongDistrictInfo(address, apiKey);
    }
}
